﻿#include "OperPhysicsPropUniform.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "GUIDialog/GUIPhyscisDialog/PhysicsDialogUniform.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyUniform.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"
namespace ModelOper
{
    bool OperPhysicsPropUniform::execGUI()
    {
        //按钮点击事件触发
        if (_emitter == nullptr) return false;
        const QString name = _emitter->objectName();
        if (name == "actionUniform")
            createPhysicsProp();
        return false;
    }
    bool OperPhysicsPropUniform::execProfession()
    {
        return false;
    }
    void OperPhysicsPropUniform::createPhysicsProp()
    {
        //弹出对话框
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        GUI::PhysicsDialogUniform dlg(GUI::PhysicsDlgExecType::Create, -1, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }
    void OperPhysicsPropUniform::duplicatePhysicsProp(const int propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::PhysicsDialogUniform dlg(GUI::PhysicsDlgExecType::Duplicate, propID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }
    void OperPhysicsPropUniform::editPhysicsProp(const int propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::PhysicsDialogUniform dlg(GUI::PhysicsDlgExecType::Edit, propID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }
    void OperPhysicsPropUniform::removePhysicsProp(const int propID)
    {
        //获取管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phys->getPhysicsPropManager();
        if (propMgr == nullptr) return;
        //移除并更新列表
        propMgr->removeDataByID(propID);
        ModelOper::updateBrowserTable();
    }
    void OperPhysicsPropUniform::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phys->getPhysicsPropManager();
        Acoustics::FITKAcousticsMaterialManager* matMgr = phys->getMaterialManager();
        if (matMgr == nullptr|| propMgr==nullptr) return;
        //设置对话框
        int type=1;
        if (attrIndex == 1)type = 2;
        GUI::AttributeSetDialog dlg(type, mw);
        dlg.setTextLabel(name);
        if (attrIndex == 1) {
            QList<Acoustics::FITKAcousticsAbsMaterial*>matList = matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::ISOSolid);
            QList<QString> list;
            for (auto value : matList) {
                list += value->getDataObjectName();
            }
            dlg.setDefaultValueQString(list);
        }
        if (attrIndex == 2) {
            dlg.setDefaultValueDouble(text.toDouble());
        }
        if (dlg.exec() != QDialog::Accepted) return;


        //材料
        QString mat = dlg.getValueQString();
        int matID = -1;
        if (attrIndex == 1) {
            matID = matMgr->getDataByName(mat)->getDataObjectID();
        }
        //厚度
        const double value = dlg.getValueDouble();

        for (int id : propID)
        {
            Acoustics::FITKPhyscisPropertyUniform* prop = dynamic_cast
                <Acoustics::FITKPhyscisPropertyUniform*>(propMgr->getDataByID(id));
            if (prop == nullptr) continue;
            //不同列设置不同参数
            switch (attrIndex)
            {
            case 1: //材料
                prop->setMaterialID(matID); break;
            case 2: //厚度
                prop->setThickness(value); break;
            default: break;
            }
        }
        ModelOper::updateBrowserTable();
    }

}